<?php


//采集控制器
class collectionController extends baseController
{

    //显示文章列表
    public function indexAction()
    {

        include CUR_VIEW_PATH . "Scollection" . DS . "collection_list.html";
    }

    //显示文章列表
    public function addAction()
    {
        //查询需要采集的数据表
        $tableM = new tableModel();
        $tableList = $tableM->all();

        include CUR_VIEW_PATH . "Scollection" . DS . "collection_add.html";
    }

    //显示文章列表
    public function editAction()
    {

        //查询需要采集的数据表
        $tableM = new tableModel();
        $tableList = $tableM->where("id in (13,14,15)")->all();
// 	    var_dump($tableList);die();
        $id = $this->common->Get('id');
        $tableName = "sl_collection";
        $_tableModel = new Model($tableName);
        $_tableDetail = $_tableModel->findOne($id);
        include CUR_VIEW_PATH . "Scollection" . DS . "collection_edit.html";
    }

    //显示文章列表
    public function filededitAction()
    {

        $id = $this->common->Get('id');

        $collectionModel = new Model("collection");
        $collectionDetail = $collectionModel->findOne($id);

        $table_id = $collectionDetail["table_id"];
        $filedModel = new filedModel();
        $filedList = $filedModel->getallFiledByTableid($table_id);

        $collectionfiledruleM = new collectionfiledruleModel();

        //组合数据
        foreach ($filedList as $k => $v) {
            $v["filedrule"] = $collectionfiledruleM->getonefiledrule($id, $v['u1']);
            $filedList[$k] = $v;

        }

        include CUR_VIEW_PATH . "Scollection" . DS . "collection_filededit.html";
    }

    // 定义insert方法，完成文章模型的插入
    public function insertAction()
    {
        //获取该用户组信息
        $data_return["status"] = false;
        $data_return["msg"] = "";
        $data_return["code"] = 1;

        $tableName = "sl_collection";
        $_tableModel = new Model("collection");
        //1.收集表单数据
        $data = $_tableModel->getFieldArray();

        //2.验证和处理
        $this->helper('input');
        $data = deepspecialchars($data);
        $data = deepslashes($data);


        //3调用模型完成入库并给出提示
        if ($_tableModel->insert($data)) {
            $data_return["status"] = true;
            $data_return["msg"] = "添加成功";
            $data_return["code"] = 0;

        } else {
            $data_return["msg"] = "添加失败";

        }
        $this->common->ajaxReturn($data_return);

    }

    // 定义insert方法，完成文章模型的插入
    public function updateAction()
    {
        //获取该用户组信息
        $data_return["status"] = false;
        $data_return["msg"] = "";
        $data_return["code"] = 1;

        $id = $this->common->Requert('id');

        $tableName = "sl_collection";
        //先获取文章信息
        $_tableModel = new Model($tableName);

        //1.收集表单数据
        $data = $_tableModel->getFieldArray();

        //2.验证和处理

        $this->helper('input');
        $data = deepspecialchars($data);
        $data = deepslashes($data);

        //3调用模型完成入库并给出提示
        if ($_tableModel->update($data)) {
            $data_return["status"] = true;
            $data_return["msg"] = "更新成功";
            $data_return["code"] = 0;

        } else {
            $data_return["msg"] = "更新失败";

        }
        $this->common->ajaxReturn($data_return);

    }

    // 定义insert方法，完成文章模型的插入
    public function updatefiledrulingAction()
    {
        //获取该用户组信息
        $data_return["status"] = false;
        $data_return["msg"] = "";
        $data_return["code"] = 1;

        $id = $this->common->Requert("collection_id");
        $collection_id = $id;
        //先获取文章信息
        $collectionModel = new Model("collection");
        $collectionDetail = $collectionModel->findOne($id);

        //接受数据表信息
        $table_id = $collectionDetail["table_id"];

        // 获得当前表名
        $tableModel = new tableModel();
        $tableName = $tableModel->getTablenameByTableid($table_id);
        //先获取文章信息
        $_tableModel = new Model($tableName);

        //1.收集表单数据
        $data = $_tableModel->getFieldArray();


        //2.验证和处理

        $this->helper('input');
        $data = deepspecialchars($data);
        $data = deepslashes($data);

        $colletionfiledruleM = new collectionfiledruleModel();

        //3调用模型完成入库并给出提示
        if ($colletionfiledruleM->updateBycollectionid($collection_id, $table_id, $data)) {
            $data_return["status"] = true;
            $data_return["msg"] = "更新成功";
            $data_return["code"] = 0;

        } else {
            $data_return["msg"] = "更新失败";

        }
        $this->common->ajaxReturn($data_return);

    }

    //定义delete方法，完成文章的删除
    public function deleteAction()
    {
        $data_return["status"] = true;
        $data_return["msg"] = "添加成功";
        $data_return["code"] = 0;

        $ids = $this->common->Requert('id');

        $tableName = "sl_collection";

        $array_id = explode(",", $ids);
        $array_id = array_unique($array_id);

        $_tableModel = new Model($tableName);


        if ($_tableModel->delete($array_id) != "false") {
            $data_return["status"] = true;
            $data_return["msg"] = "删除成功";
            $data_return["code"] = 1;

        } else {
            $data_return["msg"] = "删除成功";

        }
        $this->common->ajaxReturn($data_return);

    }

    //查询需要显示的数据
    public function gettablelistAction()
    {
        $tableName = "sl_collection";
        $_tableModel = new Model($tableName);
        $page = $this->common->Get("page");
        $limit = $this->common->Get("limit");
        $page = ($page - 1) * $limit;
        $table_sql = "";
        if ($table_sql == "") {
            $table_sql = " 1=1";
        }
        $count = $_tableModel->count();
        $_tableList1 = $_tableModel->findBySql("select * from {$tableName} where {$table_sql} limit {$page},{$limit} ");

        $_tableList["data"] = $_tableList1;
        $_tableList["code"] = "0";
        $_tableList["msg"] = "";
        $_tableList["count"] = $count;

        $this->common->ajaxReturn($_tableList);
    }

    //查询需要显示的数据
    public function docollectionAction()
    {
        $id = $this->common->Requert('id');
        $page = $this->common->Requert('page');

        //先获取信息
        $collectionModel = new collectionModel();
        //开始采集
        $collectionModel->collectLinkList($id, $page);

    }

    //测试采集数据
    public function showcollectiondataAction()
    {
        $id = $this->common->Requert('id');
        $page = $this->common->Requert('page');
        $step = $this->common->Requert('step');
        if ($step == "") {
            die("step 不能为空");
        }
        //先获取信息
        $collectionModel = new collectionModel();
        //开始采集
        $collectionModel->ShowcollectLinkListData($id, $page, $step);

    }

}